package com.tebiecloud.common.utils;

import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;

import java.util.Map;

/**
 * 响应类构建工具
 * @Author: tebie
 * @Date: 2019-07-18 14:07
 */
public class ResultBodyUtils {

    /**
     * 构建成功响应类
     * @return
     */
    public static ResultBody ok() {
        return ok(null);
    }

    public static ResultBody ok(Object data) {
        return ok(data, null);
    }

    public static ResultBody ok(Object data, Map<String, Object> extra) {
        ResultBody resultBody = new ResultBody();
        resultBody.setCode(ResultEnum.OK.getCode());
        resultBody.setMsg(ResultEnum.OK.getMessage());
        resultBody.setData(data);
        resultBody.setExtra(extra);
        return resultBody;
    }

    /**
     * 构建失败响应类
     * @return
     */
    public static ResultBody failed() {
        return failed(null);
    }

    public static ResultBody failed(Object data) {
        ResultBody resultBody = new ResultBody();
        resultBody.setCode(ResultEnum.FAIL.getCode());
        resultBody.setMsg(ResultEnum.FAIL.getMessage());
        resultBody.setData(data);
        return resultBody;
    }

    public static ResultBody failedMsg(String Msg) {
        ResultBody resultBody = new ResultBody();
        resultBody.setCode(ResultEnum.FAIL.getCode());
        resultBody.setMsg(Msg);
        resultBody.setData(null);
        return resultBody;
    }

    /**
     * 构建响应类
     * @return
     */
    public static ResultBody build(int code, String msg) {
        return build(code, msg, null, 0);
    }

    public static ResultBody build(int code, String msg, String path, int httpStatus) {
        return build(code, msg, path, httpStatus, null);
    }

    public static ResultBody build(int code, String msg, String path, int httpStatus, Object data) {
        return build(code, msg, path, httpStatus, data, null);
    }

    public static ResultBody build(int code, String msg, String path, int httpStatus, Object data, Map<String, Object> extra) {
        ResultBody resultBody = new ResultBody();
        resultBody.setCode(code);
        resultBody.setMsg(msg);
        resultBody.setPath(path);
        resultBody.setHttpStatus(httpStatus);
        resultBody.setData(data);
        resultBody.setExtra(extra);
        return resultBody;
    }

}
